Micron Document
<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Programming idiom</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Programming_idiom"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Programming_idiom rootpage-Programming_idiom skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Programming idiom</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>In <a href="Computer_programming" title="Computer programming">computer programming</a>, a <b>programming idiom</b>, <b>code idiom</b> or simply <b>idiom</b> is a <a href="Source_code" title="Source code">code</a> fragment having a <a href="Semantics_(computer_science)" title="Semantics (computer science)">semantic role</a><sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> which recurs frequently across <a href="Software" title="Software">software</a> projects. It often expresses a special feature of a recurring <a href="Language_construct" title="Language construct">construct</a> in one or more <a href="Programming_language" title="Programming language">programming languages</a>, <a href="Software_framework" title="Software framework">frameworks</a> or <a href="Library_(computing)" title="Library (computing)">libraries</a>. This definition is rooted in the linguistic definition of "<a href="Idiom" title="Idiom">idiom</a>".
</p><p>The idiom can be seen by <a href="Software_developer" class="mw-redirect" title="Software developer">developers</a> as an action on a programming concept underlying a pattern in code, which is represented in implementation by contiguous or scattered <a href="Code_snippets" class="mw-redirect" title="Code snippets">code snippets</a>. Generally speaking, a programming idiom's semantic role is a <a href="Natural_language" title="Natural language">natural language</a> expression of a simple task, <a href="Algorithm" title="Algorithm">algorithm</a>, or <a href="Data_structure" title="Data structure">data structure</a> that is not a <a href="Built-in_type" class="mw-redirect" title="Built-in type">built-in</a> feature in the programming language being used, or, conversely, the use of an unusual or notable feature that <i>is</i> built into a programming language.
</p><p>Knowing the idioms associated with a programming language and how to use them is an important part of gaining <a href="Fluency" title="Fluency">fluency</a> in that language. It also helps to transfer knowledge in the form of analogies from one language or framework to another. Such idiomatic knowledge is widely used in <a href="Crowdsourced_software_development" class="mw-redirect" title="Crowdsourced software development">crowdsourced</a> repositories to help developers overcome programming barriers.<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p><p>Mapping code idioms to <a href="Idiosyncrasies" class="mw-redirect" title="Idiosyncrasies">idiosyncrasies</a> can be a helpful way to navigate the tradeoffs between generalization and specificity. By identifying common patterns and idioms, developers can create mental models and schemata that help them quickly understand and navigate new code. Furthermore, by mapping these idioms to idiosyncrasies and specific use cases, developers can ensure that they are applying the correct approach and not overgeneralizing it.
One way to do this is by creating a reference or documentation that maps common idioms to specific use cases, highlighting where they may need to be adapted or modified to fit a particular project or development team. This can help ensure that developers are working with a shared understanding of best practices and can make informed decisions about when to use established idioms and when to adapt them to fit their specific needs.
</p><p>A common misconception is to use the <a href="Adverb" title="Adverb">adverbial</a> or <a href="Adjective" title="Adjective">adjectival</a> form of the term as "using a programming language in a typical way", which really refers to a idiosyncrasy. An idiom implies the semantics of some code in a programming language has similarities to other languages or frameworks. For example, an <i>idiosyncratic</i> way to <a href="C_dynamic_memory_allocation" title="C dynamic memory allocation">manage dynamic memory</a> in <a href="C_(programming_language)" title="C (programming language)">C</a> would be to use the <a href="C_standard_library" title="C standard library">C standard library</a> functions <code>malloc</code> and <code>free</code>, whereas <i>idiomatic</i> refers to manual <a href="Memory_management" title="Memory management">memory management</a> as recurring semantic role that can be achieved with code fragments <code>malloc</code> in C, or <code>pointer = new type [number_of_elements]</code> in C++. In both cases, the semantics of the code are intelligible to developers familiar with C or C++, once the idiomatic or idiosyncratic rationale is exposed to them. However, while idiomatic rationale is often general to the programming domain, idiosyncratic rationale is frequently tied to specific API terminology.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Examples">Examples</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Printing_Hello_World">Printing Hello World</h3></div>
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */


.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}


/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="%22Hello%2C_World!%22_program" title="&quot;Hello, World!&quot; program">"Hello, World!" program</a></div>
<p>One of the most common starting points to learn to program or notice the syntax differences between a known language and a new one.<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup>
</p><p>It has several implementations, among them the code fragments for <a href="C%2B%2B" title="C++">C++</a>:
</p>
<div class="mw-highlight mw-highlight-lang-c++ mw-content-ltr" dir="ltr"><pre><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">"Hello World</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>
</pre></div>
<p>For <a href="Java_(programming_language)" title="Java (programming language)">Java</a>:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"Hello World"</span><span class="p">);</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Inserting_an_element_in_an_array">Inserting an element in an array</h3></div>
<p>This idiom helps developers understand how to manipulate collections in a given language, particularly inserting an element <b>x</b> at a position <b>i</b> in a list <b>s</b> and moving the elements to its right.<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup>
</p><p>Code fragments:
</p><p>For <a href="Python_(programming_language)" title="Python (programming language)">Python</a>:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="n">s</span><span class="p">.</span><span class="na">insert</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">)</span>
</pre></div>
<p>For <a href="JavaScript" title="JavaScript">JavaScript</a>:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="n">s</span><span class="p">.</span><span class="na">splice</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">);</span>
</pre></div>
<p>For <a href="Perl" title="Perl">Perl</a>:
</p>
<div class="mw-highlight mw-highlight-lang-perl mw-content-ltr" dir="ltr"><pre><span class="nb">splice</span><span class="p">(</span><span class="nv">@s</span><span class="p">,</span><span class="w"> </span><span class="nv">$i</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="nv">$x</span><span class="p">)</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Algorithmic_skeleton" title="Algorithmic skeleton">Algorithmic skeleton</a></li>
<li><a href="Embedded_SQL" title="Embedded SQL">Embedded SQL</a></li>
<li><a href="Idiom_(language_structure)" title="Idiom (language structure)">Idiom</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */


.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}


/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */


.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}


/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFAllamanisSutton2014" class="citation book cs1">Allamanis, Miltiadis; Sutton, Charles (2014). "Mining idioms from source code". <i>Proceedings of the 22nd ACM SIGSOFT International Symposium on Foundations of Software Engineering</i>. pp.&nbsp;<span class="nowrap">472–</span>483. <a href="ArXiv_(identifier)" class="mw-redirect" title="ArXiv (identifier)">arXiv</a>:<span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://arxiv.org/abs/1404.0417">1404.0417</a></span>. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1145%2F2635868.2635901">10.1145/2635868.2635901</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&nbsp;<bdi>9781450330565</bdi>. <a href="S2CID_(identifier)" class="mw-redirect" title="S2CID (identifier)">S2CID</a>&nbsp;<a rel="nofollow" class="external text" href="https://api.semanticscholar.org/CorpusID:2923536">2923536</a>.</cite></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite id="CITEREFSamudioLatoza2022" class="citation book cs1">Samudio, David I.; Latoza, Thomas D. (2022). <a rel="nofollow" class="external text" href="https://cs.gmu.edu/~tlatoza/papers/BarriersInFrontendWebDevelopment.pdf">"Barriers in Front-End Web Development"</a> <span class="cs1-format">(PDF)</span>. <i>2022 IEEE Symposium on Visual Languages and Human-Centric Computing (VL/HCC)</i>. pp.&nbsp;<span class="nowrap">1–</span>11. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1109%2FVL%2FHCC53370.2022.9833127">10.1109/VL/HCC53370.2022.9833127</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&nbsp;<bdi>978-1-6654-4214-5</bdi>. <a href="S2CID_(identifier)" class="mw-redirect" title="S2CID (identifier)">S2CID</a>&nbsp;<a rel="nofollow" class="external text" href="https://api.semanticscholar.org/CorpusID:251657931">251657931</a>.</cite></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.programming-idioms.org/idiom/1/print-hello-world">"Print Hello World"</a>. <i>www.programming-idioms.org</i>.</cite></span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.programming-idioms.org/idiom/44/insert-element-in-list">"Insert element in list"</a>. <i>www.programming-idioms.org</i>.</cite></span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><a rel="nofollow" class="external text" href="https://programming-idioms.org">programming-idioms.org</a> shows short idioms implementations in most mainstream languages.</li>
<li><a class="external text external" href="https://en.wikibooks.org/wiki/C%2B%2B_Programming/Idioms">C++ programming idioms</a> from Wikibooks.</li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-01-05" href="https://en.wikipedia.org/wiki/?title=Programming_idiom&amp;oldid=1267438163">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>

</body></html>